package pe.gob.mpfn.model;

import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@Table(name = "tabla_maestra")
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "tabla_maestra")
@RooDbManaged(automaticallyDelete = true)
public class TablaMaestra {

	@OneToMany(mappedBy = "intUbigeoDiligenciaId")
    private Set<Diligencia> diligencias;

	@OneToMany(mappedBy = "intUbigeoSucesoId")
    private Set<Expediente> expedientes;

	@OneToMany(mappedBy = "intDependenciaId")
    private Set<Expediente> expedientes1;

	@OneToMany(mappedBy = "intTipoExpedienteId")
    private Set<Expediente> expedientes2;

	@OneToMany(mappedBy = "intTipoDocumentoId")
    private Set<Parte> partes;

	@OneToMany(mappedBy = "intTipoParteId")
    private Set<Parte> partes1;

	@OneToMany(mappedBy = "intCargoId")
    private Set<Personal> personals;

	@OneToMany(mappedBy = "intTipoProteccionId")
    private Set<Proteccion> proteccions;

	@OneToMany(mappedBy = "intDependenciaOrigenId")
    private Set<Proteccion> proteccions1;

	@OneToMany(mappedBy = "intDependenciaDestinoId")
    private Set<Proteccion> proteccions2;

	@OneToMany(mappedBy = "tablaMaestraIntTablaId", cascade = CascadeType.ALL)
    private Set<TablaMaestra> tablaMaestras;

	@ManyToOne
    @JoinColumn(name = "tabla_maestra_int_tabla_id", referencedColumnName = "int_tabla_id", insertable = false, updatable = false)
    private TablaMaestra tablaMaestraIntTablaId;

	@Column(name = "str_descripcion", columnDefinition = "VARCHAR", length = 100)
    @NotNull
    private String strDescripcion;

	@Column(name = "chr_estado", columnDefinition = "CHAR")
    @NotNull
    private Character chrEstado;

	public Set<Diligencia> getDiligencias() {
        return diligencias;
    }

	public void setDiligencias(Set<Diligencia> diligencias) {
        this.diligencias = diligencias;
    }

	public Set<Expediente> getExpedientes() {
        return expedientes;
    }

	public void setExpedientes(Set<Expediente> expedientes) {
        this.expedientes = expedientes;
    }

	public Set<Expediente> getExpedientes1() {
        return expedientes1;
    }

	public void setExpedientes1(Set<Expediente> expedientes1) {
        this.expedientes1 = expedientes1;
    }

	public Set<Expediente> getExpedientes2() {
        return expedientes2;
    }

	public void setExpedientes2(Set<Expediente> expedientes2) {
        this.expedientes2 = expedientes2;
    }

	public Set<Parte> getPartes() {
        return partes;
    }

	public void setPartes(Set<Parte> partes) {
        this.partes = partes;
    }

	public Set<Parte> getPartes1() {
        return partes1;
    }

	public void setPartes1(Set<Parte> partes1) {
        this.partes1 = partes1;
    }

	public Set<Personal> getPersonals() {
        return personals;
    }

	public void setPersonals(Set<Personal> personals) {
        this.personals = personals;
    }

	public Set<Proteccion> getProteccions() {
        return proteccions;
    }

	public void setProteccions(Set<Proteccion> proteccions) {
        this.proteccions = proteccions;
    }

	public Set<Proteccion> getProteccions1() {
        return proteccions1;
    }

	public void setProteccions1(Set<Proteccion> proteccions1) {
        this.proteccions1 = proteccions1;
    }

	public Set<Proteccion> getProteccions2() {
        return proteccions2;
    }

	public void setProteccions2(Set<Proteccion> proteccions2) {
        this.proteccions2 = proteccions2;
    }

	public Set<TablaMaestra> getTablaMaestras() {
        return tablaMaestras;
    }

	public void setTablaMaestras(Set<TablaMaestra> tablaMaestras) {
        this.tablaMaestras = tablaMaestras;
    }

	public TablaMaestra getTablaMaestraIntTablaId() {
        return tablaMaestraIntTablaId;
    }

	public void setTablaMaestraIntTablaId(TablaMaestra tablaMaestraIntTablaId) {
        this.tablaMaestraIntTablaId = tablaMaestraIntTablaId;
    }

	public String getStrDescripcion() {
        return strDescripcion;
    }

	public void setStrDescripcion(String strDescripcion) {
        this.strDescripcion = strDescripcion;
    }

	public Character getChrEstado() {
        return chrEstado;
    }

	public void setChrEstado(Character chrEstado) {
        this.chrEstado = chrEstado;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "int_tabla_id", columnDefinition = "INT")
    private Integer intTablaId;

	public Integer getIntTablaId() {
        return this.intTablaId;
    }

	public void setIntTablaId(Integer id) {
        this.intTablaId = id;
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new TablaMaestra().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countTablaMaestras() {
        return entityManager().createQuery("SELECT COUNT(o) FROM TablaMaestra o", Long.class).getSingleResult();
    }

	public static List<TablaMaestra> findAllTablaMaestras() {
        return entityManager().createQuery("SELECT o FROM TablaMaestra o", TablaMaestra.class).getResultList();
    }

	public static TablaMaestra findTablaMaestra(Integer intTablaId) {
        if (intTablaId == null) return null;
        return entityManager().find(TablaMaestra.class, intTablaId);
    }

	public static List<TablaMaestra> findTablaMaestraEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM TablaMaestra o", TablaMaestra.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            TablaMaestra attached = TablaMaestra.findTablaMaestra(this.intTablaId);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public TablaMaestra merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        TablaMaestra merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
}
